﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestDataGenerator
{
    class Generator
    {
        public decimal GenerateFrom = 0;
        public decimal GenerateTo = 0;
        public string PathToScript = "";
        public DBHandler dbHandler;
        public string ExceptionText;
        public string ScriptText;

        // Процедура инициализации скрипта
        public void InitScript()
        {
            FileInfo file = new FileInfo(PathToScript);
            try
            {
                ScriptText = file.OpenText().ReadToEnd();
            }
            catch (Exception e)
            {
                ExceptionText = e.Message;
            }
        }
        
        // Процедура вызова скрипта генерации экземпляра объекта
        public void GenerateData(int CurrentPosition)
        {
            //System.Threading.Thread.Sleep(500);
            string paramScriptText = string.Format(ScriptText, CurrentPosition);
            ExceptionText = null;
            SqlConnection conn = dbHandler.connection;
            using (var cmd = new SqlCommand(paramScriptText, conn))
            {
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException e)
                {
                    ExceptionText = e.Message + " (line: "+ e.LineNumber +")";
                }
                catch (Exception e)
                {
                    ExceptionText = e.Message;
                }
            }
        }
    }
}
